/*************************************************
Author: will
Date: 10/11/2012
Description:
Notes:
*************************************************/

#ifndef __CEquipMgr_h__
#define __CEquipMgr_h__

#include "CTArray.h"
#include "CEquipConfig.h"
#include "commclass.h"
#include "../storage/CUserStorage.h"
#include "CUserEquip.h"
#include "pb_cfg_equip.pb.h"
#include "../item/CDropItemSubType.h"
#include "MsgComm.pb.h"

namespace CTLib
{
    class CEnpNetHead;
}
#define INVALID_EQUIP_ID  0xFFFFFFFF
class CPlayerOnlineData;
class PBMsgHead;
class CShopBuyBackItem;
class pb_cfg_equip;
class MsgEquipAttr;
class CPlayerEquipData;
class CScrollConfigData;
class MsgAnsUserStorageInfo;
class MsgAnsItemDetailInfos;
class MsgAnsEquipRefine;
class MsgAnsEquipBless;
class pb_cfg_potential;

class CEquipMgr
{
public:
	CEquipMgr()
    {
	}

	virtual ~CEquipMgr()
    {
	}

    static int CountSize()
    {
        return CEquipConfig::CountSize();
    }

    int Init(const char* sBuff)
    {
        return _stEquipConfig.Init(sBuff);
    }

	int InitData(CUserEquip & stUserEquip, unsigned int uiGID, CEquipData & stData);
    int GetData(CUserEquip& stUserEquip, CEquipData& stData);
	int LoadEquipConfig(pb_cfg_equip & stEquipPbCfg);
	int LoadSuiteConfig(pb_cfg_equip & stEquipPbCfg);
	int LoadStrengthConfig(pb_cfg_equip & stEquipPbCfg);
    int LoadPotentialConfig(pb_cfg_potential & stPbCfg);
	int LoadConfig(const char* szCfgPath, const char* szPotenPath);

    int AddConfigData(CEquipConfigData& stConfigData);
    int GetConfigData(unsigned int uiEquipID, CEquipConfigData& stConfigData);
    int GetConfigData(CDropItemSubTypeConfigData::T_DROP_ITEM_SUBTYPE_CONFIG_KEY& szKey,
        				   CEquipConfigData& stConfigData);
	int AddSuiteConfig(CEquipSuiteCfg & stConfigData);
	int GetSuiteConfig(unsigned char ucSuiteID, CEquipSuiteCfg& stConfigData);
	int AddStrengthConfig(CEquipStrengthCfg & stConfigData);
	int GetStrengthConfig(CStrengthCfgKey & stKey, CEquipStrengthCfg& stConfigData);
	int GetScrollEquipCfgId(unsigned short usScrollID, unsigned short &usEquipConfigID);

    int GetEquipStrengthPrice(unsigned short usEquipConfigID, unsigned char ucCurStrength, int& iSilver);

    //获取售店价格
    int GetEquipSellPrice(unsigned int uiEquipCfgId, unsigned int uiStrength, int & iSilverPrice);

	//为初次登陆的用户分配装备
	int AllocUserFirstEquip(unsigned char ucCareer, CUserEquip& stUserEquip, CUserStorage & stUserStorage);

	int AddEquip(CUserEquip& stUserEquip, unsigned short usEquipConfigID, CUserStorage & stUserStorage, unsigned int & uiEquipID);
	int AddBuyBackEquip(CShopBuyBackItem &stBuyBackItem, CUserEquip & stUserEquip, CUserStorage &stUserStorage);
    int DelEquip(CUserEquip& stUserEquip, unsigned int uiEquipID);
    int GetEquip(CUserEquip& stUserEquip, unsigned int uiEquipID, CSingleEquip& stEquip);

    //检查装备上是否有魔晶等附件. 无附件返回0,　否则返回非0
    int CheckEquipAddOns(CSingleEquip & stEquip);

	//穿、脱装备
	int WearEquip(CPlayerDetailInfo & stPlayerDetail, CUserEquip& stUserEquip, unsigned int uiEquipCfgID, short shResID, unsigned int &uiEquipID);
    int WearEquip(CUserEquip& stUserEquip, unsigned int uiEquipID, unsigned int uiPartnerId);
    int TakeOffEquip(CUserEquip& stUserEquip, unsigned int uiEquipID);

    //强化装备
    int StrengthenEquip(CUserEquip& stUserEquip, unsigned int uiEquipID, int &iSilverPrice);

	int GetConfigIdByEquipId(unsigned int uiEquipId, unsigned short &usConfigId);

	//获取装备属性
	int GetEquipAttr(CUserEquip& stUserEquip, unsigned int uiPartnerId, CPlayerAttr& stEquipAttr);

    //计算魔晶加成后的属性. (bOnlyBase: 是否只修正基础属性)
    int GetCrystalAttr(CUserEquip& stUserEquip, unsigned int uiPartnerId, bool bOnlyBase, CPlayerAttr& stPlayerAttr);

    //获取套装属性
    int GetSuiteAttr(CUserEquip& stUserEquip, unsigned int uiPartnerId, CPlayerAttr& stSuiteAttr);

    //获取魔晶潜能属性
    int GetPotenAttr(CUserEquip& stUserEquip, unsigned char ucPotenLevel, unsigned int uiPartnerId, CPlayerAttr& stPotenAttr);

    //更新魔晶潜能等级
    void UpdatePotenLevel (CPlayerBaseInfo & stBaseInfo);

    //潜能等级是否已满
    int GetPotenUpNeedExp (CPlayerBaseInfo & stBaseInfo);

	//获取单件装备的属性
	int GetEquipAttr(unsigned short usEquipConfigID, unsigned short usStrengthLevel, CSingleEquipAttr &stEquipAttr);
	int GetEquipAttr(unsigned short usEquipConfigID, unsigned short usStrengthLevel, MsgEquipAttr &stEquipAttr);

    //获取装备的战斗力
    int GetEquipFighting(CSingleEquip & stSingleEquip, int & iFighting);

	//获取角色身上的所有装备
    typedef CTLib::CTArray<CSingleEquip, MAX_PARTNER_EQUIP_NUM> T_EQUIP_ITEMS;
	int GetEquipList(CUserEquip& stUserEquip, unsigned int uiRoleId, T_EQUIP_ITEMS &astEquip);

    int GetStrengthTotalLevel (CUserEquip& stUserEquip, unsigned int uiRoleId);

	//根据装备配置ID,在管理器中生成新的装备
	int CreateEquip(CUserEquip &stUserEquip, unsigned int uiEquipCfgId, CSingleEquip& stEquip);

    uint64 MakeEquipKey(unsigned int uiGid, unsigned int uiEquipID) const
    {
        return CT_UINT64_MAKE(uiGid, uiEquipID);
    }

	//装备合成,stEquip传入合成前的装备. 返回合成后的装备.
	int MakeEquip (CUserEquip & stUserEquip, CScrollConfigData &stScrollConfig, CSingleEquip& stEquip);
	int GetNewEquipStrength(unsigned short usCurStrength, unsigned short &usNewStrength);

    //镶嵌魔晶
    int InsetCrystal (CPlayerDetailInfo & stPlayerDetailInfo, CPlayerOnlineData & stPlayerOnlineData, unsigned char ucCrystalId, unsigned int uiEquipId, char & chCrystalPos);

    //拆除魔晶
    int UnsetCrystal (CUserEquip & stUerEquip, unsigned char ucCrystalId, unsigned int uiEquipId, char & chCrystalPos);

    //装备转换
    int CheckTransform (CUserEquip & stUserEquip, unsigned int uiEquipId, unsigned short usNewConfigid);
    int Transform (CUserEquip & stUserEquip, unsigned int uiEquipId, unsigned short usNewConfigid, unsigned int & uiNewEquipId);

    //获取装备摘要信息
    int GetEquipInfo(CPlayerDetailInfo& stPlayerDetail, CPlayerOnlineData& stPlayerOnlineData, unsigned int uiRoleId, MsgAnsUserStorageInfo& stAns);
    int GetEquipInfo(CPlayerDetailInfo& stPlayerDetail, CPlayerOnlineData& stPlayerOnlineData, unsigned int uiRoleId, ::google::protobuf::RepeatedPtrField<MsgGridItemInfo>& stEquips);

    int GetEquipInfo(unsigned int uiGid, CPlayerEquipData & stEquip, MsgGridItemInfo & stGridItemInfo);


    typedef CTArray<CAttrPair, MAX_SUITE_ATTR_PER_EQUIP> T_EQUIP_SUITE_ATTR;
    int GetSingleEquipSuiteAttr (CUserEquip& stUserEquip, unsigned int uiEquipId, unsigned char & ucSuiteId, T_EQUIP_SUITE_ATTR & stSuiteAttr);
    void GetEquipDetailInfo(CPlayerOnlineData& stPlayerOnlineData, const ::google::protobuf::RepeatedField<int64>& stReqIDList,
        MsgAnsItemDetailInfos& stAns);

    //返回信息:主角和伙伴的装备信息
    void SendEquipInfoToFront(CPlayerDetailInfo& stPlayerDetail, CPlayerOnlineData& stPlayerOnlineData, unsigned int uiRoleId,
                                PBMsgHead stMsgHead, CEnpNetHead stEnpHead);

    void SendEquipDetailInfoToFront(CPlayerOnlineData& stPlayerOnlineData, const ::google::protobuf::RepeatedField<int64>& stReqIDList,
                                    PBMsgHead stMsgHead, CEnpNetHead stEnpHead);

    void NotifyEquipInMap (unsigned int uiEquipId, unsigned int uiGid);

    //装备洗练：获取当前装备信息
    int RefineEquipGetInfo(CUserEquip& stUserEquip, unsigned int uiEquipID, MsgAnsEquipRefine& stAns);

    //装备洗练：激活属性
    int RefineEquipEnvalidAttr(CUserEquip& stUserEquip, unsigned int uiEquipID, CPlayerBaseInfo& stPlayerBaseInfo, bool& bIsWare, short& shWareRoleID);

    //装备洗练: roll属性和等级  bIsEnable:是否是激活属性
    int RefineEquipRollAttrAndLevel(unsigned char& ucAttrID, unsigned char& ucAttrLevel);

    //装备洗练: 洗练
    int RefineEquip(CPlayerOnlineData& stPlayerOnlineData, CPlayerBaseInfo& stPlayerBaseInfo, unsigned int uiEquipID, int iLockedAttrPos);

    //装备洗练：保存洗练结果
    int RefineEquipSave(CUserEquip& stUserEquip, unsigned int uiEquipID, bool& bIsWare, short& shWareRoleID);

    //饰品祝福：获取信息
    int BlessEquipGetInfo(CUserEquip& stUserEquip, unsigned int uiEquipID, CPlayerDetailInfo& stPlayerDetailInfo, MsgAnsEquipBless& stAns);

    //饰品祝福: 祝福
    int BlessEquip(CUserEquip& stUserEquip, unsigned int uiEquipID, CPlayerBaseInfo& stPlayerBaseInfo, 
                    CPlayerDetailInfo& stPlayerDetailInfo, bool& bIsWare, short& shWareRoleID);

    //饰品祝福: 提升属性
    int BlessEquipLevelUp(CUserEquip& stUserEquip, unsigned int uiEquipID, CPlayerDetailInfo& stPlayerDetailInfo, 
                    bool& bIsWare, short& shWareRoleID);

    //获取洗练和祝福属性
    int GetRefineAndBlessAttr(CSingleEquip& stEquip, CPlayerAttr& stEquipAttr);
public:
    CEquipConfig _stEquipConfig;
    CRefineAndBlessConfig _stRefineAndBlessConfig;
};


#endif // __CEquipMgr_h__

